package src.C_io;

import java.io.*;

// 转化流(字节流转化为字符流)
public class Demo12 {
    public static void main(String[] args) throws IOException {
        /*
         两种 以指定的字符编码来读写文件 方法，第一种在jdk11之后就淘汰了，但是我们当前项目用的是jdk8
            // 第一种，利用转化流
            InputStreamReader isr = new InputStreamReader(new FileInputStream("src/C_io/c.txt"),"UTF-8");
            OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("src/C_io/c.txt"),"UTF-8");
            // 第二种，；利用字符流
            FileReader fr = new FileReader("src\\C_io\\c.txt", Charset.forName("UTF-8"));
            FileWriter fw = new FileWriter("src\\C_io\\c.txt", Charset.forName("UTF-8"));

         ps：！！！千万不要在idea中手动改文件编码！！！ 切记~ 切记~

        */

        // 案例：将本地文件中的GBK文件，转成UTF-8，且每次读一行，不能乱码
        BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("src/C_io/e.txt"),"GB18030"));
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("src/C_io/d.txt"),"UTF-8"));
        String str;
        while ((str = br.readLine()) != null){
            bw.write(str);
            bw.newLine();
        }
        bw.close();
        br.close();
    }
}
